# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: OSAMC <https://github.com/osam-cologne/archlinux-proaudio>
# Contributor: Christopher Arndt <aur -at- chrisarndt -dot- de>

pkgname=python-rtmidi
pkgver=1.5.5
pkgrel=1
pkgdesc='Python bindings for the MIDI I/O library RtMidi'
arch=('loong64' 'x86_64')
url='https://github.com/SpotlightKid/python-rtmidi'
license=('MIT')
depends=(
  'glibc'
  'gcc-libs'
  'python'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'meson-python'
  'ninja'
  'cython'
  'alsa-lib'
  'jack'
  'python-sphinx'
  'python-myst-parser'
)
_commit='877a23fa8084e85bbe5b5333bbdf21e3674bab6a'
source=(
  "$pkgname::git+$url#commit=$_commit"
  'github.com-SpotlightKid-rtmidi::git+https://github.com/SpotlightKid/rtmidi'
  'use-system-ninja.patch'
  'remove-xdg-open.patch'
  'skip-broken-clean.patch'
)
b2sums=('SKIP'
        'SKIP'
        '89690e60c1a5fd983e6f08a83957e100b361a2ee7e560194fa2f09989712084c36a2e3ae5b7196a1a5ab3e3d335b783ffc4bb58aa31888a55aac7495fa2ec2a1'
        '2c030e9d3c7b392a8a041165418b8012d0d5372217b515eb764943eefeb4ad7fa49e7fc462e53004106e8cbbf6ce9beb088603b58478812c870fccea6a723289'
        'a5fa68ddcac7e5e0fcbfd05cfaf9fe0279f5f84fac6daca6a426a27c42514aabd20d9871da4b3a073f6add2f3431c410fc0155d64639d332a1616a3971c6cb91')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # use forked/vendored rtmidi
  git submodule init
  git config submodule.src/rtmidi.url "$srcdir/github.com-SpotlightKid-rtmidi"
  git -c protocol.file.allow=always submodule update

  # remove xdg-open from Makefile
  patch -p1 -i "$srcdir/remove-xdg-open.patch"

  # skip broken clean in Makefile
  patch -p1 -i "$srcdir/skip-broken-clean.patch"

  # use system Ninja
  patch -p1 -i "$srcdir/use-system-ninja.patch"
}

build() {
  cd "$pkgname"

  # build python module
  python -m build --wheel --no-isolation

  # documentation
  make docs
}

package() {
  depends+=('libasound.so' 'libjack.so')
  cd "$pkgname"

  python -m installer --destdir="$pkgdir" dist/*.whl

  # license
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s "$site_packages/${pkgname/-/_}-$pkgver.dist-info/LICENSE.md" \
    "$pkgdir/usr/share/licenses/$pkgname/LICENSE.md"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md
  cp -vr docs/_build/html "$pkgdir/usr/share/doc/$pkgname"
  cp -vr examples "$pkgdir/usr/share/doc/$pkgname"
}
